home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / mmgr / MM_StarTrack.lha / MM / Rexx / MM_ST_ConvertCfg.rexx < prev    next >
OS/2 REXX Batch file  |  1996-05-24  |  10KB  |  503 lines

  1. /*
  2.  
  3.                     $VER: MM_ST_ConvertCfg 0.95  (23.05.96)
  4.  
  5.                             (C) 1996 Robert Hofmann
  6.  
  7. */
  8.  
  9. parse arg args
  10.  
  11. options cache
  12. options failat 99
  13. options results
  14.  
  15. signal on break_c
  16. signal on break_d
  17. signal on break_e
  18. signal on break_f
  19. signal on halt
  20. signal on ioerr
  21. signal on syntax
  22.  
  23. address 'MAILMANAGER'
  24.  
  25.  
  26. Main:
  27.  
  28.     call Init
  29.     call Header
  30.     call Parse_Args(strip(args))
  31.  
  32.     call Convert_Cfg()
  33.  
  34. call Quit(0, 'All done.')
  35. exit
  36.  
  37.  
  38. break_c:; break_d:; break_e:; break_f:; halt:
  39.  
  40.     signal off break_c
  41.     signal off break_d
  42.     signal off break_e
  43.     signal off break_f
  44.     signal off halt
  45.  
  46.     return_code        =    5
  47.     error_line    = 0
  48.     error_msg            = 'Execution halted!!!'
  49.     rc                        = 0
  50. signal Exit
  51.  
  52.  
  53. Convert_Cfg: procedure Expose system.
  54.  
  55.     if system.arg.old=''    then    old_cfg    = system.prg.cfg
  56.     else                                                old_cfg    = system.arg.old
  57.  
  58.     if system.arg.new=''    then    new_cfg    = system.prg.cfg
  59.     else                                                new_cfg    = system.arg.new
  60.  
  61.     MM_ReadStem old_cfg 'cfg'
  62.     if RC~=0 then call Quit(30, 'Unable to read "'old_cfg'"!')
  63.  
  64.     search    = '#REMAP(FROM|TO)'
  65.     remap.    = 0
  66.     MM_SearchInStem 'cfg' 'remap' '"?'search'#?"' 'NUM'
  67.  
  68.     if remap.count=0 then call Quit(5, 'No "'search'"-statements present - Nothing to convert...')
  69.  
  70.     LF        = 'a'x
  71.     line    = ';----------------------------------------------------------------------'LF ||,
  72.                     ';'LF ||,
  73.                     '; #REMAP(FROM|TO)'LF ||,
  74.                     '; Name        OLD/K,NEW/K'LF ||,
  75.                     '; Address     OLD/K,NEW/K'LF ||,
  76.                     '; Flags       FLAGS/A'LF ||,
  77.                     '; AddInfo'LF ||,
  78.                     '; Reply'LF || LF
  79.  
  80.     do n=0 to remap.count-1
  81.         nr    = remap.n
  82.  
  83.         say 'Processing line' right(nr, 3)':' cfg.nr
  84.  
  85.         call Parse_Cfg_Line(nr)
  86.         call Parse_Cfg_Args(args, 'OLDADDR/A,NEWADDR/A,ADDINFO/S,REPLY/S', key, l)
  87.  
  88.         parse var cfg.prm.oldaddr cfg.prm.oldaddr . '%' cfg.prm.oldname
  89.         parse var cfg.prm.newaddr cfg.prm.newaddr . '%' cfg.prm.newname
  90.  
  91.         line    = line || key || LF
  92.         line    = line || left('Address', 13) left('OLD' cfg.prm.oldaddr, max(length(cfg.prm.oldaddr)+5, 29)) 'NEW' cfg.prm.newaddr || LF
  93.  
  94.         if cfg.prm.oldname||cfg.prm.newname~='' then
  95.             do
  96.                 line = line || left('Name', 13)
  97.  
  98.                 if cfg.prm.oldname~='' then
  99.                     do
  100.                         tmp        = 'OLD "'translate(cfg.prm.oldname, ' ', '_')'"'
  101.                         line    = line left(tmp, max(length(tmp)+1, 29))
  102.                     end
  103.  
  104.                 if cfg.prm.newname~='' then line = line 'NEW "'translate(cfg.prm.newname, ' ', '_')'"'
  105.  
  106.                 line = strip(line) || LF
  107.             end
  108.  
  109.         if cfg.prm.addinfo    then line = line || 'AddInfo'LF
  110.         if cfg.prm.reply        then line    = line || 'Reply'LF
  111.  
  112.         cfg.nr    = line
  113.         line        = ''
  114.     end
  115.  
  116.     if upper(old_cfg)=upper(new_cfg) then    MM_MoveFile    new_cfg new_cfg'.old'
  117.  
  118.     MM_WriteStem    new_cfg 'cfg'
  119.  
  120. return
  121.  
  122.  
  123. Exit:
  124.  
  125.     select
  126.         when return_code>=40 then error = 'INTERNAL-ERROR:'
  127.         when return_code>=30 then error = 'IO-ERROR:'
  128.         when return_code>=20 then error = 'ERROR:'
  129.         when return_code>=10 then error = 'WARNING:'
  130.         when return_code>=5  then error = 'INFO:'
  131.         otherwise                                    error = ''
  132.     end
  133.  
  134.     tmp    = '***' strip(error error_msg) '***'
  135.  
  136.     say
  137.     say '***' strip(error error_msg) '***'
  138.     say
  139.  
  140.   call Log()
  141.     call Log(tmp, '+')
  142.     call Log(,'\')
  143.  
  144.     call setclip('MM_LogPre', system.mm.logpre)
  145.  
  146. exit return_code
  147.  
  148.  
  149. Get_Arg: procedure Expose args system.
  150.  
  151.   arg keyword, mode, old
  152.  
  153.     uargs    = upper(args)
  154.     p            = find(uargs, keyword)
  155.  
  156.     if p=0    then
  157.         do
  158.             p = pos(' 'keyword'=', ' 'uargs)
  159.  
  160.             if p>0 then    args    = overlay(' ', args, p+length(keyword))
  161.  
  162.       p = find(upper(args), keyword)
  163.         end
  164.  
  165.     system.cmdopt.keyword    = p>0
  166.  
  167.     select
  168.         when mode=0    then
  169.             if p>0 then
  170.                 do
  171.                     ret        = 1
  172.                     args    = delword(args, p, 1)
  173.                 end
  174.             else ret    = old
  175.  
  176.         when mode=1 then
  177.             if p>0 then
  178.                 do
  179.                     left    = subword(args, 1, p-1)
  180.                     rest    = subword(args, p+1)
  181.  
  182.                     if left(rest, 1)='"' then    parse var rest . '"'    ret '"'    rest
  183.                     else                                            parse var rest                ret            rest
  184.  
  185.                     args    = strip(left strip(rest))
  186.                 end
  187.             else ret    = old
  188.  
  189.         when mode=2 then
  190.             do
  191.                 if left(args, 1)='"'    then    parse var args . '"'    ret '"'    args
  192.                 else                                                parse var args                ret         args
  193.  
  194.                 if strip(ret)=''            then    ret = old
  195.             end
  196.  
  197.         otherwise exit 99
  198.     end
  199.  
  200.     args    = strip(args)
  201.     ret        = strip(ret, 'b', '" ')
  202.  
  203. return ret
  204.  
  205.  
  206. Get_Version: procedure
  207.  
  208.     parse arg mode
  209.  
  210.     parse value sourceline(3-mode) with . . ver .
  211.     parse var ver tst 'ß' .
  212.  
  213.     if ~datatype(strip(tst, 'b', '/c '), 'N') then
  214.         if ~mode then ver = Get_Version(1)
  215.         else exit 99
  216.  
  217. return ver
  218.  
  219.  
  220. Header:
  221.  
  222.     call Log(,'/')
  223.     call Log('***' system.prg.id '***', '+')
  224.     call Log(' 'system.prg.cr)
  225.     call Log()
  226.  
  227. return
  228.  
  229.  
  230. Init:
  231.  
  232.     system.                        = 0
  233.  
  234.     system.prg.ver        = Get_Version(0)
  235.     system.prg.name        = 'MM_ST_ConvertCfg'
  236.     system.prg.id            = system.prg.name 'v'system.prg.ver
  237.     system.prg.cfg        = 'MM:Config/MM_StarTrack/MM_StarTrack.cfg'
  238.     system.prg.cr            = '(C) 1996 Robert Hofmann'
  239.     system.tmpfile        = 'T:'system.prg.name'.tmp'
  240.     system.mm.logpre     = getclip('MM_LogPre')
  241.     system.prg.logpre    = system.mm.logpre'|'
  242.     call                                setclip('MM_LogPre', system.prg.logpre)
  243.     system.cmdopts        = 'OLD,NEW'
  244.  
  245.     call Include_Lib('rexxsupport')
  246. return
  247.  
  248.  
  249. Include_Lib: procedure Expose system.
  250.  
  251.     parse arg lib, prio
  252.     if right(upper(lib), 8)~='.LIBRARY' then lib    = lib'.library'
  253.     if prio=''                                                    then prio    = 0
  254.  
  255.     if ~exists('libs:'lib)                            then call Quit(30, '"'lib'" does not exist!')
  256.     if ~show('l', lib) then
  257.         if ~addlib(lib, prio, -30, 0)            then call Quit(20, 'Could not open "'lib'"!')
  258.  
  259. return
  260.  
  261.  
  262. IOerr:
  263.  
  264.     signal off ioerr
  265.  
  266.     return_code        = 20
  267.     error_line    = sigl
  268.     error_msg            = 'IO-error' rc 'at line' sigl '['errortext(rc)']')
  269.     rc                        = 0
  270. signal Exit
  271.  
  272.  
  273. Log: procedure Expose system.
  274.  
  275.     parse arg text, pre, level
  276.  
  277.     if ~datatype(level, 'N') then level = system.prg.loglevel
  278.  
  279.     tmp        = word('PRG MM', (pre~='')+1)
  280.     text    = system.tmp.logpre || pre' 'text
  281.  
  282.     MM_WriteLog 'text' level
  283. return
  284.  
  285.  
  286. Parse_Args: procedure Expose system.
  287.  
  288.     parse arg args
  289.  
  290.     tpl        = system.cmdopts',?/S'
  291.     args    = translate(args, '  ', '9'x'=')
  292.  
  293.     pk        = pos('/K', tpl)
  294.     ps        = pos('/S', tpl)
  295.  
  296.     select
  297.         when pk=0    & ps=0    then    p    = 0
  298.         when pk=0 & ps>0    then  p    = ps
  299.         when ps=0 & pk>0    then    p    = pk
  300.         otherwise                                p    = min(pk, ps)
  301.     end
  302.  
  303.     p            = lastpos(',', left(tpl, p))
  304.     tpl        = substr(tpl',', p+1) || left(tpl, max(p-1, 0))
  305.  
  306.     do while tpl~=''
  307.         parse var tpl template ',' tpl
  308.         parse var template keyword '/' .
  309.  
  310.         bool    = pos('/S',    template)>0
  311.         key        = pos('/K', template)>0
  312.         must    = pos('/A', template)>0
  313.         num        = pos('/N', template)>0
  314.  
  315.         select
  316.             when must then        system.arg.keyword    = '0'x
  317.             when bool    then        system.arg.keyword    = 0
  318.             when num    then        system.arg.keyword    = 0
  319.  
  320.             otherwise                    system.arg.keyword    = ''
  321.         end
  322.  
  323.         if bool | key    then    mode    = ~bool
  324.         else                                mode    = 2
  325.  
  326.         system.arg.keyword    = Get_Arg(keyword, mode, system.arg.keyword)
  327.  
  328.         if keyword='?' & system.arg.keyword=1 then leave
  329.  
  330.         if must & system.arg.keyword='0'x then
  331.             do
  332.                 tmp    = template 'missing!!!'
  333.  
  334.                 say
  335.                 say ' ***' tmp '***'
  336.  
  337.                 signal Usage
  338.             end
  339.  
  340.         if num & ~datatype(system.arg.keyword, 'N') then
  341.             if ~must & system.arg.keyword='' then    system.arg.keyword    = 0
  342.             else
  343.                 do
  344.                     tmp    = 'Numeric value expected for' template', but is "'system.arg.keyword'"!!!'
  345.  
  346.                     say
  347.                     say ' ***' tmp '***'
  348.  
  349.                     signal Usage
  350.                 end
  351.     end
  352.  
  353.     tmp    = '?'; if system.arg.tmp then signal Usage
  354.  
  355.     if args~='' then call Quit(10, 'Unknown option(s):' args)
  356.  
  357. return
  358.  
  359.  
  360. Parse_Cfg_Args: procedure Expose cfg. system.
  361.  
  362.     parse arg args, tpl, cfgkey, l
  363.  
  364.     args    = strip(translate(args, '  ', '9'x'='))
  365.  
  366.     if args=''    then call Quit(15, 'No arguments given for' cfgkey 'at line' l'!!!')
  367.  
  368.     pk        = pos('/K', tpl)
  369.     ps        = pos('/S', tpl)
  370.  
  371.     select
  372.         when pk=0    & ps=0    then    p    = 0
  373.         when pk=0 & ps>0    then  p    = ps
  374.         when ps=0 & pk>0    then    p    = pk
  375.         otherwise                                p    = min(pk, ps)
  376.     end
  377.  
  378.     p            = lastpos(',', left(tpl, p))
  379.     tpl        = substr(tpl',', p+1) || left(tpl, max(p-1, 0))
  380.  
  381.     do while tpl~=''
  382.         parse var tpl template ',' tpl
  383.         parse var template keyword '/' .
  384.  
  385.         bool    = pos('/S',    template)>0
  386.         key        = pos('/K', template)>0
  387.         must    = pos('/A', template)>0
  388.         num        = pos('/N', template)>0
  389.  
  390.         select
  391.             when must then        cfg.prm.keyword    = '0'x
  392.             when bool    then        cfg.prm.keyword    = 0
  393.             when num    then        cfg.prm.keyword    = 0
  394.  
  395.             otherwise                    cfg.prm.keyword    = ''
  396.         end
  397.  
  398.         if bool | key    then    mode    = ~bool
  399.         else                mode    = 2
  400.  
  401.         cfg.prm.keyword    = Get_Arg(keyword, mode, cfg.prm.keyword)
  402.  
  403.         if must & cfg.prm.keyword='0'x then call Quit(15, template 'for' cfgkey 'missing at line' l)
  404.  
  405.         if num & ~datatype(cfg.prm.keyword, 'N') then
  406.             if ~must & cfg.prm.keyword='' then    cfg.prm.keyword    = 0
  407.             else                                                                call Quit(15, 'Numeric value expected for'cfgkey template' at line' l', but is "'cfg.prm.keyword'"!!!')
  408.     end
  409.  
  410.     if args~='' then call Quit(10, 'Unknown option(s) "'args'" for' cfgkey 'at line' l'!!!')
  411.  
  412. return
  413.  
  414.  
  415. Parse_Cfg_Line:
  416.  
  417.         parse arg l
  418.         parse value    strip(translate(cfg.l, ' ', '9'x)) with key args ';' .
  419.  
  420.         key        = upper(strip(key))
  421.         args    = strip(args)
  422.         uargs    = upper(args)
  423.         l            = l+1
  424.  
  425. return
  426.  
  427.  
  428. Quit:
  429.  
  430.     parse arg return_code, error_msg
  431.  
  432.     error_line    = 0
  433.     rc                        = 0
  434. signal Exit
  435.  
  436.  
  437. Read_Cfg: procedure Expose system.
  438.  
  439.     MM_ReadStem system.prg.cfg 'cfg'
  440.     if RC~=0 then call Quit(31, 'Unable to read' system.prg.cfg'!!!')
  441.  
  442.     call Log('Reading config...')
  443.  
  444.     cnt    = 0
  445.  
  446.     do l=0 to cfg.count-1
  447.         parse value strip(translate(cfg.l, ' ', '9'x)) with key args ';' .
  448.         key        = upper(strip(key))
  449.         args    = strip(args)
  450.  
  451.         select
  452.             when key=''                then iterate
  453.  
  454.             when key='#...'        then ...
  455.             [...]
  456.  
  457.  
  458.             otherwise say '*** CFG-ERROR: Unknown keword "'key'" at line' l'!!!'
  459.         end
  460.  
  461.         cnt    = cnt+1
  462.     end
  463.  
  464. return
  465.  
  466.  
  467. Replace: procedure
  468.  
  469.     parse arg string, new, old
  470.  
  471.     do while index(string, old) ~= 0
  472.         interpret "parse var string l '"old"' r"
  473.         string = l || new || r
  474.     end
  475.  
  476. return string
  477.  
  478.  
  479. Syntax:
  480.  
  481.     signal off syntax
  482.  
  483.     return_code        = 40
  484.     error_line    = sigl
  485.     error_msg            = 'Syntax-error' rc 'at line' sigl '['errortext(rc)']'
  486.     rc                        = 0
  487. signal Exit
  488.  
  489.  
  490. Usage:
  491.  
  492.     rx.            = ''
  493.     rx.0.0    = '[rx] '
  494.     rx.0.1    = '[.rexx]'
  495.     m                =    pos('/e', system.prg.ver)>0
  496.  
  497.     say
  498.     say 'Usage:' rx.m.0 || system.prg.name || rx.m.1 system.cmdopts
  499.     say
  500. call Quit(0, 'Usage requested.')
  501.  
  502.  
  503.